package com.biboheart.adapter.support.utils;

import com.biboheart.adapter.compiler.ast.Node;
import com.biboheart.adapter.compiler.enums.NodeType;

import java.util.ArrayList;
import java.util.List;

public class NodeUtils {
    // 取变量
    public static List<String> listVariable(Node node) {
        if (null == node) {
            return null;
        }
        List<String> variableList = new ArrayList<>();
        exploreNodeVariable(node, variableList);
        return variableList;
    }

    private static void exploreNodeVariable(Node node, List<String> list) {
        if (null == node) {
            return;
        }
        if (NodeType.NODE_VARIABLE.equals(node.getNodeType())) {
            list.add(String.valueOf(node.getToken().getVal()));
        }
        List<Node> children = node.getChildren();
        if (null == children || children.isEmpty()) {
            return;
        }
        for (Node child : children) {
            exploreNodeVariable(child, list);
        }
    }
}
